﻿
var viewModel = function () {
    var self = this;
    this.refreshClick = function () {
        window.location.reload();
    };
    this.addClick = function () {
        self.gridEdit.addnew({});
    };
    this.editClick = function () {
        var row = self.grid.datagrid('getSelected');
        var index = self.grid.datagrid('getRowIndex', row);
        self.gridEdit.begin(index, row);
    };
    this.deleteClick = function () {
        self.gridEdit.deleterow();
    };
    this.grid = {
        size: { w: 189, h: 40 },
        url: "/api/sys/testeek",
        queryParams: ko.observable(),
        pagination: true,
        customLoad: false
    };
    this.gridEdit = new com.editGridViewModel(this.grid);
    this.grid.onDblClickRow = self.gridEdit.begin;
    this.grid.onClickRow = self.gridEdit.ended;
    this.grid.OnAfterCreateEditor = function (editors, row) {
        if (row._isnew == undefined)
            com.readOnlyHandler('input')(editors.UserCode.target, true);
    };
    this.tree = {
    //    method: 'GET',
    //    url: '/api/sys/organize',
    //    loadFilter: function (d) {
    //        var filter = utils.filterProperties(d, ['OrganizeCode as id', 'OrganizeName as text', 'ParentCode as pid']);
    //        return utils.toTreeData(filter, 'id', 'pid', 'children');
    //    },
    //    onSelect: function (node) {
    //        self.grid.queryParams({ OrganizeCode: node.id });
    //    }
    };
    this.saveClick = function () {
        self.gridEdit.ended();
        var post = {};
        post.list = self.gridEdit.getChanges(['UserCode', 'UserName', 'MaterialCode', 'Description', 'IsEnable']);
        if (self.gridEdit.ended() && post.list._changed) {
            com.ajax({
                url: '/api/sys/user/edit',
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', '保存成功！');
                    self.gridEdit.accept();
                }
            });
        }
    };
    this.passwordClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请先选择一个用户！');
        com.message('confirm', '确定要把选中用户的密码重置为<span style="color:red">1234</span>吗？', function (b) {
            if (b) {
                com.ajax({
                    type: 'POST',
                    url: '/api/sys/user/postresetpassword/' + row.UserCode,
                    success: function () {
                        com.message('success', '密码已重置成功！');
                    }
                });
            }
        });
    };
};
ko.bindingViewModel(new viewModel());